package consultan.vanke.com.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import consultan.vanke.com.R;


/**
 * 自定义Toolbar
 */
public class SimpleToolbar extends Toolbar {
    /**
     * 左侧Title
     */
    private ImageView mTxtLeftTitle;
    private TextView txt_left_titles;
    /**
     * 中间Title
     */
    private TextView mTxtMiddleTitle;
    /**
     * 右侧Title
     */
    private TextView mTxtRightTitle;
    private TextView txt_right_title_copy;//应对多个图标
    private TextView txt_right_title_red;//对应红点

    private Toolbar toolbar_common;

    public SimpleToolbar(Context context) {
        this(context, null);
    }

    public SimpleToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_simple_toolbar, this);
        mTxtLeftTitle = (ImageView) findViewById(R.id.txt_left_title);
        mTxtMiddleTitle = (TextView) findViewById(R.id.txt_main_title);
        mTxtRightTitle = (TextView) findViewById(R.id.txt_right_title);
        txt_right_title_red = (TextView) findViewById(R.id.txt_right_title_red);
        txt_right_title_copy = (TextView) findViewById(R.id.txt_right_title_copy);
        toolbar_common = (Toolbar) findViewById(R.id.toolbar_common);
        txt_left_titles = (TextView) findViewById(R.id.txt_left_titles);
    }

    public void setLeftImageGone() {
        mTxtLeftTitle.setVisibility(View.GONE);
    }

    public void setGone() {
        this.setVisibility(View.GONE);
    }

    public void setMainBg(int color) {
        toolbar_common.setBackgroundColor(color);
        mTxtLeftTitle.setImageDrawable(getResources().getDrawable(R.mipmap.back_black));
        mTxtMiddleTitle.setTextColor(getResources().getColor(R.color.color_0C0C0C));
    }

    //设置中间title的内容
    public void setMainTitle(String text) {
        this.setTitle(" ");
        mTxtMiddleTitle.setVisibility(View.VISIBLE);
        mTxtMiddleTitle.setText(text);
    }

    public void setLeftTextShow(String text) {
        txt_left_titles.setVisibility(View.VISIBLE);
        txt_left_titles.setText(text);
    }

    //设置中间title的内容文字的颜色
    public void setMainTitleColor(int color) {
        mTxtMiddleTitle.setTextColor(color);
    }

    //设置title左边文字
    public void setLeftTitleText(String text) {
        mTxtLeftTitle.setVisibility(View.VISIBLE);
//        mTxtLeftTitle.setText(text);
    }

    //设置title左边文字颜色
    public void setLeftTitleColor(int color) {
//        mTxtLeftTitle.setTextColor(color);
    }

    //设置title左边图标
    public void setLeftTitleDrawable(int res) {
        Drawable dwLeft = ContextCompat.getDrawable(getContext(), res);
        dwLeft.setBounds(0, 0, dwLeft.getMinimumWidth(), dwLeft.getMinimumHeight());
//        mTxtLeftTitle.setCompoundDrawables(dwLeft, null, null, null);
    }

    //设置title左边图标
    public void setLeftTitleDrawableSingle(int res) {
        mTxtLeftTitle.setImageDrawable(getResources().getDrawable(res));
    }

    //设置title左边点击事件
    public void setLeftTitleClickListener(OnClickListener onClickListener) {
        mTxtLeftTitle.setOnClickListener(onClickListener);
    }

    public void setLeftTitleTextClickListener(OnClickListener onClickListener) {
        txt_left_titles.setOnClickListener(onClickListener);
    }

    //设置title右边文字
    public void setRightTitleText(String text) {
        txt_right_title_copy.setVisibility(View.VISIBLE);
        txt_right_title_copy.setText(text);
        txt_right_title_copy.setCompoundDrawables(null, null, null, null);
    }


    //设置title右边文字
    public String getRightTitleText() {
        return txt_right_title_copy.getText().toString();
    }


    //设置title右边文字
    public void setRightAginTitleText(String text) {
        mTxtRightTitle.setVisibility(View.VISIBLE);
        mTxtRightTitle.setText(text);
        mTxtRightTitle.setCompoundDrawables(null, null, null, null);
    }

    //设置title右边文字颜色
    public void setRightTitleColor(int color) {
        txt_right_title_copy.setTextColor(color);
    }

    public void setRightAginTitleColor(int color) {
        mTxtRightTitle.setTextColor(color);
    }

    //设置title右边图标
    public void setRightTitleDrawable(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTxtRightTitle.setCompoundDrawables(null, null, dwRight, null);
        mTxtRightTitle.setVisibility(View.VISIBLE);
        txt_right_title_red.setVisibility(View.GONE);
    }

    //设置title右边图标
    public void setRightTitleDrawableAndRed(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTxtRightTitle.setCompoundDrawables(null, null, dwRight, null);
        mTxtRightTitle.setVisibility(View.VISIBLE);
        txt_right_title_red.setVisibility(View.VISIBLE);
    }

    //设置title右边图标COPY
    public void setRightTitleAginDrawable(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        txt_right_title_copy.setCompoundDrawables(null, null, dwRight, null);
        txt_right_title_copy.setVisibility(View.VISIBLE);
    }


    //设置title右边点击事件
    public void setRightTitleClickListener(OnClickListener onClickListener) {
        mTxtRightTitle.setOnClickListener(onClickListener);
    }

    public void setRightTitleAginClickListener(OnClickListener onClickListener) {
        txt_right_title_copy.setOnClickListener(onClickListener);
    }
}
